from time import gmtime, strftime

from threading import Thread
from multiprocessing import Process, Queue, SimpleQueue
import queue, logging
import timeit

from MarketData.quote import Quote

from GUI.output import PATH


def summarize_Quote(pairquote, commodity_cur, base_cur):
    price = 0
    count = 0
    for platf in pairquote:
        if platf[0] == commodity_cur and platf[1] == base_cur:
            price += (pairquote[platf].price_bid + pairquote[platf].price_ask) / 2.0
            count += 1
    price /= count

    return price

def Quote_multithread(platform, commodity_cur, base_cur, level):
    response = dict()
    que = dict()

    threads = []
    for platf in platform:
        que[platf] = Queue()
        thread_order = Thread(target=Quote_thread, 
                              args=(platf, commodity_cur, base_cur, level, que[platf]))
        thread_order.start()
        threads.append(thread_order)
        
    for platf in platform:
        response[platf] = que[platf].get()

    for thread_order in threads:
        thread_order.join()

    return response


def Quote_multithread_mid(platform, commodity_cur, base_cur, mid_cur, level):
    response = dict()
    que = Queue()

    threads = []
    for platf in platform:
        thread_order = Thread(target=Quote_thread,
                              args=(platf, commodity_cur, base_cur, level, que))

        thread_order_comm_mid = Thread(target=Quote_thread,
                                       args=(platf, commodity_cur, mid_cur, level, que))

        thread_order_mid_base = Thread(target=Quote_thread,
                                       args=(platf, mid_cur, base_cur, level, que))

        thread_order.start()
        thread_order_comm_mid.start()
        thread_order_mid_base.start()

        threads.append(thread_order)
        threads.append(thread_order_comm_mid)
        threads.append(thread_order_mid_base)

    for i in range(0, len(platform)*3):
        try:
            item = que.get(timeout=60)
            if 'error' not in item:
                response.update(item)
        except Exception as e:
            logging.info('quote_multithread_get_timeout : ' + str(e))

    for thread_order in threads:
        try:
            thread_order.join(timeout=60)
        except Exception as e:
            logging.info('quote_multithread_join_timeot : ' + str(e))

    return response


class Quote_thread:
    def __init__(self,
                 platf, commodity_cur, base_cur, level,
                 que):
        try:
            start = timeit.default_timer()

            que.put({(commodity_cur, base_cur, platf): Quote(platf, commodity_cur, base_cur, level)})

            stop = timeit.default_timer()
            logging.info('%-10s%-10.2f' % (platf, stop - start))
        except Exception as e:
            que.put({"error": "error"})
            logging.info([e, gmtime()])
            # book = open(PATH.ROOTFOLDER + '/error_log.txt', 'a')
            # book.write(strftime("%Y-%m-%d %H:%M:%S" + str(e) + "\r\n", gmtime()))
            # book.close()